package com.duan.ad.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.duan.ad.pojo.Ad;
import com.duan.ad.service.AdService;
import com.duan.ad.mapper.AdMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
* @author lenovo
* @description 针对表【ad(广告表)】的数据库操作Service实现
* @createDate 2024-07-31 15:20:48
*/
@Service
public class AdServiceImpl extends ServiceImpl<AdMapper, Ad>
    implements AdService{

    @Autowired
    private AdMapper adMapper;

    @Override
    public List<Ad> selectAll() {
        LambdaQueryWrapper<Ad> queryWrapper=new LambdaQueryWrapper<>();
        return adMapper.selectList(queryWrapper);
    }

    @Override
    public List<Ad> selectList(Ad ad) {
        LambdaQueryWrapper<Ad> queryWrapper=this.queryWrapper(ad);
        return adMapper.selectList(queryWrapper);
    }

    @Override
    public Integer insert(Ad ad) {
        return adMapper.insert(ad);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try{
            String[] split=ids.split(",");
            for (String id : split) {
                Ad ad=new Ad();
                ad.setId(Long.parseLong(id));
                ad.setStatus(1);
                adMapper.updateById(ad);
            }
            return 1;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public Integer update(Ad ad) {
        return adMapper.updateById(ad);
    }

    @Override
    public Page<Ad> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<Ad>) this.selectAll();
    }

    @Override
    public Page<Ad> pageList(Ad ad, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<Ad>) this.selectList(ad);
    }

    public LambdaQueryWrapper<Ad> queryWrapper(Ad ad){
        LambdaQueryWrapper <Ad> queryWrapper = new LambdaQueryWrapper<>();
        if (ad.getId() != null){
            queryWrapper.eq(Ad::getId,ad.getId());
        }
        if (ad.getName() != null){
            queryWrapper.eq(Ad::getName,ad.getName());
        }
        if (ad.getPosition() != null){
            queryWrapper.eq(Ad::getPosition,ad.getPosition());
        }
        if (ad.getStartTime() != null){
            queryWrapper.eq(Ad::getStartTime,ad.getStartTime());
        }
        if (ad.getEndTime() != null){
            queryWrapper.eq(Ad::getEndTime,ad.getEndTime());
        }
        if (ad.getStatus() != null){
            queryWrapper.eq(Ad::getStatus,ad.getStatus());
        }
        if (ad.getImage() != null){
            queryWrapper.eq(Ad::getImage,ad.getImage());
        }
        if (ad.getUrl() != null){
            queryWrapper.eq(Ad::getUrl,ad.getUrl());
        }
        if (ad.getRemarks() != null){
            queryWrapper.eq(Ad::getRemarks,ad.getRemarks());
        }
        if (ad.getIsDelete() != null){
            queryWrapper.eq(Ad::getIsDelete,ad.getIsDelete());
        }
        if (ad.getCreateTime() != null){
            queryWrapper.eq(Ad::getCreateTime,ad.getCreateTime());
        }
        if (ad.getUpdateTime() != null){
            queryWrapper.eq(Ad::getUpdateTime,ad.getUpdateTime());
        }
        return queryWrapper;
    }
}




